// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './app'
import routers from './router'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import store from './vuex'
import Login from './utils/login'
import cookie from './utils/cookie'
import * as wechat from './utils/util.wechat'

// import VueAwesomeSwiper from 'vue-awesome-swiper'
// require('swiper/dist/css/swiper.css')
Vue.config.productionTip = false

Vue.use(VueRouter)
Vue.use(VueResource)
// Vue.use(VueAwesomeSwiper)
// 创建一个路由对象用于管理页面的路由
const router = new VueRouter({
  mode: 'history',
  routes: routers
})
// 目前只支持微信环境
router.beforeEach((to, from, next) => {
    // 首页判断微信静默登陆
  console.log(to.query, cookie.get('openid'), process.env.NODE_ENV)
  if (navigator.userAgent.toLowerCase().indexOf('micromessenger') > -1 || typeof navigator.wxuserAgent !== 'undefined') {
    if (!cookie.get('openid') && typeof to.query.code !== 'undefined') {
      if (to.query.state === 'base') Login.baseGetOpenid(to.query)
      else Login.snsapiGetOpenid(to.query) // 授权登陆
    } else if (!cookie.get('openid') && typeof to.query.code === 'undefined') {
      cookie.set('beforeLoginUrl', to.fullPath) // 保存用户进入的url
      console.log('需要授权登陆')
      wechat.authLogin()
    }
  }
  next()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
  http: {
    root: '/root',
    headers: {
      Authorization: 'yezi'
    }
  }
})
